package SqlConnect;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/*密码信息*/
public class SqlPassword {
    private final Statement st;

    public SqlPassword(Statement st) throws SQLException {
        this.st = st;
        st.execute(
                "CREATE TABLE IF NOT EXISTS `login` (`name` VARCHAR(30) NOT NULL,`password` VARCHAR(30) NOT NULL,PRIMARY KEY(`name`))ENGINE=InnoDB DEFAULT CHARSET=utf8;");
    }

    public boolean sign_in(String name, String password) {
        try {
            assert name != null;
            assert password != null;
            if (name.isEmpty() || password.isEmpty())
                return false;
            st.execute("INSERT INTO login (name,password) VALUES ('" + name + "', '" + password + "');");
        } catch (SQLException e) {
            return false;
        }
        return true;
    }

    public boolean log_in(String name, String password) {
        try {
            assert name != null;
            assert password != null;
            if (name.isEmpty() || password.isEmpty())
                return false;
            ResultSet res = st.executeQuery("SELECT * FROM login WHERE name='" + name.strip() + "';");
            if (res.next()) {
                return password.equals(res.getString("password"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
        return false;
    }
}
